// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.export;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dengues.model.warehouse.ProcessType;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-4-11 qiang.zhang $
 * 
 */
public class ExportFileResource {

    private ProcessType item;

    // string is the relative path of the directoryName, where to store the resources in List.
    private Map<String, Set<URL>> map = new HashMap<String, Set<URL>>();

    private IPath path;

    /**
     * Adds the resource list to this.
     * 
     * @param relativePath relative Path
     * @param resources
     */
    public void addResources(String relativePath, List<URL> resources) {
        Set<URL> storeList = map.get(relativePath);
        if (storeList == null) {
            storeList = new HashSet<URL>(resources);
            map.put(relativePath, storeList);
        } else {
            storeList.addAll(resources);
        }
    }

    public void removeResources(String relativePath, URL resource) {
        Set<URL> storeList = map.get(relativePath);
        if (storeList == null) {
            return;
        } else {
            storeList.remove(resource);
        }
    }

    public void addResources(List<URL> resources) {
        addResources("", resources); //$NON-NLS-1$
    }

    public ProcessType getItem() {
        return item;
    }

    public void setProcess(ProcessType process) {
        this.item = process;
    }

    public ExportFileResource(ProcessType item, String path) {
        super();
        this.item = item;
        this.path = new Path(path);
    }

    /**
     * Answer the total number of file resources that exist at or below self in the resources hierarchy.
     * 
     * @return int
     * @param checkResource org.eclipse.core.resources.IResource
     */
    protected int countChildrenOf(String checkResource) throws CoreException {

        File file = new File(checkResource);

        if (file.isFile()) {
            return 1;
        }

        int count = 0;

        if (file.isDirectory()) {
            File[] children = file.listFiles();
            for (int i = 0; i < children.length; i++) {
                count += countChildrenOf(children[i].getPath());
            }
        }
        return count;
    }

    public Set<String> getRelativePathList() {
        return map.keySet();
    }

    public Set<URL> getResourcesByRelativePath(String path) {
        return map.get(path);
    }

    /**
     * Gets the count of the files.
     * 
     * @return
     */
    public int getFilesCount() throws CoreException {
        Set<String> paths = getRelativePathList();
        int result = 0;
        for (Iterator iter = paths.iterator(); iter.hasNext();) {
            String path = (String) iter.next();
            Set<URL> resource = getResourcesByRelativePath(path);
            for (Iterator iterator = resource.iterator(); iterator.hasNext();) {
                URL url = (URL) iterator.next();
                result += countChildrenOf(url.getPath());
            }
        }
        return result;
    }

    /**
     * Getter for path.
     * 
     * @return the path
     */
    public IPath getPath() {
        return this.path;
    }

    /**
     * Sets the path.
     * 
     * @param path the path to set
     */
    public void setPath(IPath path) {
        this.path = path;
    }
}
